package com.hs.li.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hs.bai.mapper.OrderMapper;
import com.hs.bai.pojo.Order;
import com.hs.ding.mapper.TransportMapper;
import com.hs.ding.mapper.VehicleMapper;
import com.hs.ding.pojo.Transport;
import com.hs.ding.pojo.Vehicle;
import com.hs.li.mapper.CarCarrierMapper;
import com.hs.li.pojo.CarCarrier;
import com.hs.li.pojo.CarManagement;
import com.hs.li.mapper.CarManagementMapper;
import com.hs.li.service.ICarManagementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 汽车管理 服务实现类
 * </p>
 *
 * @author lhj
 * @since 2022-06-13
 */
@Service
public class CarManagementServiceImpl extends ServiceImpl<CarManagementMapper, CarManagement> implements ICarManagementService {

    @Autowired
    private CarCarrierMapper carCarrierMapper;

    @Autowired
    private CarManagementMapper carManagementMapper;

    //    注入派车单mapper
    @Autowired
    private VehicleMapper vehicleMapper;
    //    注入需求单mapper
    @Autowired
    private TransportMapper transportMapper;

    @Override
    public Integer searchLastCarNumber() {

        return carManagementMapper.selectLastCarNumber();
    }

    /**
     * 根据传入的carrierNumber（承运商编号）查询车辆集合
     *
     * @param val
     * @return
     */
    @Override
    public List<CarManagement> searchCarManagementBycarrierNumber(Integer val) {
        LambdaQueryWrapper<CarManagement> carManagementLambdaQueryWrapper = new LambdaQueryWrapper<>();
        carManagementLambdaQueryWrapper.eq(CarManagement::getCarrierNumber, val);

        List<CarManagement> list = carManagementMapper.selectList(carManagementLambdaQueryWrapper);

        return list;
    }

    @Override
    public List<CarManagement> searchCarManagementByCarrierName(String carrierName) {

        LambdaQueryWrapper<CarCarrier> carrierLambdaQueryWrapper = new LambdaQueryWrapper<>();
        carrierLambdaQueryWrapper.eq(CarCarrier::getCarrierName, carrierName);
        CarCarrier carCarrier = carCarrierMapper.selectOne(carrierLambdaQueryWrapper);

        LambdaQueryWrapper<CarManagement> carManagementLambdaQueryWrapper = new LambdaQueryWrapper<>();
        carManagementLambdaQueryWrapper.eq(CarManagement::getCarStatus, 0);
        carManagementLambdaQueryWrapper.eq(CarManagement::getCarrierNumber, carCarrier.getCarrierNumber());

        List<CarManagement> list = carManagementMapper.selectList(carManagementLambdaQueryWrapper);

        return list;
    }

    @Override
    public List<CarManagement> searchCarManagementByCondition(Integer currentPage, Integer pageSize, String condition, Integer typeId) {
        System.out.println("currentPage = " + currentPage);
        System.out.println("condition = " + condition);
        if (currentPage > 0) {
            currentPage = --currentPage;
        }
        String carPlate = "";
        Integer carNumber = 0;

        if (StringUtils.hasText(condition)) {
            //判断参数是否包含中文
            if (condition.matches("^[\\u4e00-\\u9fa5][a-zA-Z_0-9]1*")) {
                carPlate = condition;
            }

            if (condition.matches("[0-9]+")) {
                carNumber = Integer.parseInt(condition);
            }
        }

        System.out.println("carPlate = " + carPlate);
        System.out.println("carNumber = " + carNumber);

        List<CarManagement> list = carManagementMapper.selectByCondition(currentPage, pageSize, carPlate, carNumber, typeId);
        System.out.println("conditionList = " + list);

        return list;
    }


    /**
     * 订单完成时修改车辆使用状态
     *
     * @param orderId 订单id
     * @return
     */
    @Override
    public boolean changeCarTypeByOrderId(String orderId) {
        //1. 判断传入orderId是否为空，不为空执行修改操作
        if (StringUtils.hasText(orderId)) {
            //2. 根据orderId查询对应的 Vehicle 派车单对象
            LambdaQueryWrapper<Vehicle> vehicleQW = new LambdaQueryWrapper<>();
            vehicleQW.eq(Vehicle::getOrderId, orderId);

            Vehicle vehicle = vehicleMapper.selectOne(vehicleQW);

            //3. 根据vehicle对象的id 查询对应的 Transport需求单对象
            LambdaQueryWrapper<Transport> transportQW = new LambdaQueryWrapper<>();
            transportQW.eq(Transport::getvOrderId, vehicle.getvOrderId());

            Transport transport = transportMapper.selectOne(transportQW);
            
            //4. 根据transport对象的车牌号码 查询对应的CarManagement车辆信息对象，修改车辆使用状态
            LambdaQueryWrapper<CarManagement> carManagementQW = new LambdaQueryWrapper<>();
            carManagementQW.eq(CarManagement::getCarPlate, transport.getCarPlate());

            CarManagement carManagement = carManagementMapper.selectOne(carManagementQW);
            //把状态修改为0 -> 未使用
            carManagement.setCarStatus(0);

            //5. 把修改后的对象保存到数据库
            int i = carManagementMapper.updateById(carManagement);

            return i > 0;

        }


        return false;
    }

    @Override
    public boolean changeCarTypeByCarplate(String carPlate) {

        if (StringUtils.hasText(carPlate)) {
            LambdaQueryWrapper<CarManagement> carManagementQW = new LambdaQueryWrapper<>();
            carManagementQW.eq(CarManagement::getCarPlate, carPlate);

            CarManagement carManagement = carManagementMapper.selectOne(carManagementQW);
            //把选择的车辆 使用状态改为1
            carManagement.setCarStatus(1);

            int i = carManagementMapper.updateById(carManagement);

            return i > 0;

        }

        return false;
    }


    @Override
    public boolean changeCarTypeByTransportId(String transportId) {

        if (StringUtils.hasText(transportId)) {
            //1. 根据运输单号 查询车辆车牌
            LambdaQueryWrapper<Transport> transportQW = new LambdaQueryWrapper<>();
            transportQW.eq(Transport::gettOrderId, transportId);

            Transport transport = transportMapper.selectOne(transportQW);

            //2. 根据车牌 查询对应 车辆信息，修改车辆状态
            LambdaQueryWrapper<CarManagement> carManagementQW = new LambdaQueryWrapper<>();
            carManagementQW.eq(CarManagement::getCarPlate, transport.getCarPlate());

            CarManagement carManagement = carManagementMapper.selectOne(carManagementQW);
            carManagement.setCarStatus(0);

            //3. 把修改后的车辆信息 保存到数据库
            int i = carManagementMapper.updateById(carManagement);

            return i > 0;

        }
        return false;
    }
}
